package storage

import (
	. "go-caipu/pkg/services/sqlstore/migrator"
)

func AddFileStorageMigrations(mg *Migrator) {
	fileStorage := Table{
		Name: "sys_file_storage",
		Columns: []*Column{
			{Name: "id", Type: DB_BigInt, IsPrimaryKey: true, IsAutoIncrement: true},
			{Name: "file_name", Type: DB_Varchar, Length: 255, Nullable: false, Comment: "原始文件名"},
			{Name: "file_key", Type: DB_Varchar, Length: 255, Nullable: false, Comment: "存储系统中的唯一标识/key"},
			{Name: "file_size", Type: DB_BigInt, Nullable: false, Comment: "文件大小(字节)"},
			{Name: "file_type", Type: DB_Varchar, Length: 100, Nullable: true, Comment: "文件类型(MIME)"},
			{Name: "file_ext", Type: DB_Varchar, Length: 20, Nullable: true, Comment: "文件扩展名"},
			{Name: "storage_type", Type: DB_Varchar, Length: 30, Nullable: false, Comment: "存储类型'LOCAL','MINIO','ALIYUN','TENCENT'"},
			{Name: "bucket_name", Type: DB_Varchar, Length: 100, Nullable: true, Comment: "存储桶名称"},
			{Name: "storage_path", Type: DB_Varchar, Length: 500, Nullable: true, Comment: "存储路径(不包含文件名)"},
			{Name: "access_url", Type: DB_Varchar, Length: 500, Nullable: true, Comment: "访问URL"},
			{Name: "md5", Type: DB_Varchar, Length: 32, Nullable: true, Comment: "文件MD5"},
			{Name: "sha1", Type: DB_Varchar, Length: 40, Nullable: true, Comment: "文件SHA1"},
			{Name: "status", Type: DB_TinyInt, Default: "1", Nullable: true, Comment: "状态(1:正常,0:删除)"},
			{Name: "created_by", Type: DB_BigInt, Length: 100, Nullable: true, Comment: "上传用户"},
			{Name: "created_at", Type: DB_DateTime, Nullable: false, Default: "CURRENT_TIMESTAMP", Comment: "创建时间"},
			{Name: "updated_at", Type: DB_DateTime, Nullable: false, Default: "CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP", Comment: "更新时间"},
			{Name: "deleted_at", Type: DB_DateTime, Nullable: true},
		},
		Indices: []*Index{
			{Cols: []string{"file_key"}, Type: UniqueIndex, Name: "uk_file_key"},
			{Cols: []string{"md5"}, Type: UniqueIndex, Name: "uk_file_md5"},
			//{Cols: []string{"created_by"}, Type: IndexType, Name: "idx_created_by"},
		},
	}

	// 创建表
	mg.AddMigration("create table sys_file_storage ", NewAddTableMigration(fileStorage))
	mg.AddMigration("create index uk_file_storage_key", NewAddIndexMigration(fileStorage, fileStorage.Indices[0]))
	mg.AddMigration("create index uk_file_storage_md5", NewAddIndexMigration(fileStorage, fileStorage.Indices[1]))
}
